﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Reflection;
using UnityEditorInternal;
using System;
using Util;
namespace Util.Editor
{
    [CustomPropertyDrawer(typeof(SortingLayerAttribute))]
    public class SortingLayerDrawer : UnityEditor.PropertyDrawer
    {
        const string NONE = "<None>";

        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property.propertyType == SerializedPropertyType.String)
            {
                position = EditorGUI.PrefixLabel(position, label);
                string value = property.stringValue;
                if (string.IsNullOrEmpty(value))
                    value = NONE;
                if (GUI.Button(position, value, EditorStyles.popup))
                {
                    Selector(property);
                }
            }
            else
            if (property.propertyType == SerializedPropertyType.Integer)
            {
                SortingLayerField(label, property, EditorStyles.popup, EditorStyles.label);

            }
            else
            {
                EditorGUI.LabelField(position, "ERROR:", "可能只适用于string或者Int");
            }
        }
        public static void SortingLayerField(GUIContent label, SerializedProperty layerID, GUIStyle style, GUIStyle labelStyle)
        {
            MethodInfo methodInfo = typeof(EditorGUILayout).GetMethod("SortingLayerField", BindingFlags.Static | BindingFlags.NonPublic, null, new[] { typeof(GUIContent), typeof(SerializedProperty), typeof(GUIStyle), typeof(GUIStyle) }, null);

            if (methodInfo != null)
            {
                object[] parameters = new object[] { label, layerID, style, labelStyle };
                methodInfo.Invoke(null, parameters);
            }
        }

        void Selector(SerializedProperty property)
        {
            string[] layers = GetSortingLayerNames();

            GenericMenu menu = new GenericMenu();

            bool isNone = string.IsNullOrEmpty(property.stringValue);
            menu.AddItem(new GUIContent(NONE), isNone, HandleSelect, new TBag<string, SerializedProperty>(NONE, property));

            for (int i = 0; i < layers.Length; i++)
            {
                string name = layers[i];
                menu.AddItem(new GUIContent(name), name == property.stringValue, HandleSelect, new TBag<string, SerializedProperty>(name, property));
            }
            menu.ShowAsContext();
        }

        void HandleSelect(object val)
        {
            var pair = (TBag<string, SerializedProperty>)val;
            if (pair.t1.Equals(NONE))
            {
                pair.t2.stringValue = "";
            }
            else
            {
                pair.t2.stringValue = pair.t1;
            }
            pair.t2.serializedObject.ApplyModifiedProperties();
        }

        // Get the sorting layer names
        public string[] GetSortingLayerNames()
        {
            Type internalEditorUtilityType = typeof(InternalEditorUtility);
            PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
            return (string[])sortingLayersProperty.GetValue(null, new object[0]);
        }
    }
}